package com.ziqing.apeletnews.retrofit;



import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.ziqing.apeletnews.App;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Retrofit2
 * Created by pokermman on 2017/8/1.
 */

public class RetrofitManger {


    private static NewApiService SERVICE;
    /**
     * 请求超时时间
     */
    private static final int DEFAULT_TIMEOUT = 10 * 1000;

    //短缓存有效期为1分钟
    public static final int CACHE_STALE_SHORT = 60;
    //7天
    private static final int CACHE_STALE_LONG = 60 * 60 * 24 * 7;

    public static NewApiService getDefault() {
        if (SERVICE == null) {
            // 指定缓存路径,缓存大小100Mb
            Cache cache = new Cache(new File(App.getMyApplication().getCacheDir(), "HttpCache"),
                    1024 * 1024 * 100);
            ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(),
                    new SharedPrefsCookiePersistor(App.getMyApplication()));
            /**打印数据*/
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//打印
            /**添加统一的token参数*/
            CommonInterceptor commonInterceptor = new CommonInterceptor();

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .cache(cache)
                    .cookieJar(cookieJar)
                    .retryOnConnectionFailure(true)
                    .addInterceptor(headerInterceptor)
                    .addInterceptor(commonInterceptor)
                    .addInterceptor(httpLoggingInterceptor)
                    .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .build();

            SERVICE = new Retrofit.Builder()
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加RXJAVA
                    .baseUrl("http://193.112.129.54/index.php/index/")
                    .build().create(NewApiService.class);
        }
        return SERVICE;
    }


    /**
     * 对所有请求添加请求头
     */

    private static final Interceptor headerInterceptor = chain -> {
        Request request = chain.request();
        Response originalResponse = chain.proceed(request);
//                    return originalResponse.newBuilder().build();
        return originalResponse.newBuilder()
                .header("key1", "value1")
                .addHeader("key2", "value2")
                .build();
    };

}
